<!DOCTYPE html>
<html lang="eh">

<head>
    <meta charset="UTF-8">
    <title>登录页面</title>
    <script src="/common/common-js.js" type="application/javascript"></script>
    <style>
        .login-box {
            border: 1px solid #DCDFE6;
            width: 350px;
            margin: 100px auto;
            padding: 35px 35px 15px 35px;
            border-radius: 5px;
            -webkit-border-radius: 5px;
            -moz-border-radius: 5px;
            box-shadow: 0 0 25px #909399;
        }

        .login-title {
            text-align: center;
            margin: 0 auto 40px auto;
            color: #303133;
        }
    </style>

</head>

<body>
<div id="app">
    <!-- ref：类似于id，给表单给个名字 model对应的实体对象 rule验证规则 -->
    <el-form ref="loginForm" :model="form" :rules="rules" label-width="60px" class="login-box">
        <h3 class="login-title">问卷管理</h3>
        <!-- prop指定每一项验证规则 -->
        <el-form-item label="账号" prop="adminName">
            <!-- 双向绑定数据 -->
            <el-input prefix-icon="el-icon-user-solid" type="text" placeholder="请输入账号" v-model="form.adminName"/>
        </el-form-item>
        <el-form-item label="密码" prop="password">
            <el-input prefix-icon="el-icon-s-opportunity" type="password" placeholder="请输入密码" v-model="form.password"/>
        </el-form-item>
        <div style="text-align: center">
            <!-- 点击调用onSubmit方法 -->
            <el-button style="width:150px;" type="primary" v-on:click="onSubmit('loginForm')">登录</el-button>
        </div>
    </el-form>
</div>
</body>
<script type="text/javascript">
    new Vue({
        el: "#app",
        data: {
            form: {
                adminName: '',
                password: '',
            },
            rules: { //校验规则
                adminName: [
                    {required: true, message: '请输入用户名', trigger: 'blur'}
                ],
                password: [
                    {required: true, message: '请输入密码', trigger: 'blur'}
                ]
            }
        },
        methods: {
            //loginForm
            onSubmit(formName) {
                // 为表单绑定验证功能，this.$refs 获取所有ref的集合
                this.$refs[formName].validate((valid) => {
                    //验证成功
                    if (valid) {
                        //访问地址，提交的数据
                        axios.post("/user/login", this.form).then(resp=>{
                            //获取服务器返回的数据
                            let resultInfo = resp.data;
                            //注：success没有括号，idea会加上括号
                            if (resultInfo.success) {
                                //跳转到address.html页面
                                location.href = "/pages/address.html";
                            }
                            else {
                                //登录失败：显示服务器返回的信息
                                this.$message.error(resultInfo.message);
                            }
                        })
                    } else {
                        //验证失败，不提交表单
                        return false;
                    }
                });
            }
        }
    });
</script>
</html>